package com.hzit.web.controller;

import com.hzit.proxy.base.ImageQRCode;
import com.hzit.proxy.entity.UserEntity;
import com.hzit.proxy.sendmsg.MsgType;
import com.hzit.proxy.sendmsg.SendMessageProxy;
import com.hzit.proxy.user.UserInfoProxy;
import com.hzit.proxy.user.UserListProxy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Controller
public class MainController {


    @RequestMapping("/main")
    public String main(Model model) {

        //二维码地址
        String imageUrl = ImageQRCode.getImageUrl();

        UserListProxy userListProxy = new UserListProxy();
        List<String> userList = userListProxy.getUserList(null);

        UserInfoProxy userInfoProxy = new UserInfoProxy();
        List<UserEntity> userEntityList = userInfoProxy.getUserListInfo(userList);

        //指定排序规则
        Collections.sort(userEntityList, new Comparator<UserEntity>() {
            @Override
            public int compare(UserEntity o1, UserEntity o2) {
                return (int) (o2.getSubscribe_time()-o1.getSubscribe_time());
            }
        });

        model.addAttribute("imageUrl", imageUrl);
        model.addAttribute("userList", userEntityList);
        return "main";
    }

    /**
     * 发送消息
     *
     * @param openid
     * @param msgtype
     * @param content
     * @param title
     * @param desc
     * @param url
     * @param picurl
     * @return 1:发送成功   0:发送失败
     */
    @PostMapping("/send/kfmsg")
    @ResponseBody
    public int sendKfMst(@RequestParam(value = "openid", required = true) String openid,
                         @RequestParam(value = "msgtype", required = true, defaultValue = "text") String msgtype,
                         @RequestParam(value = "content", required = false) String content,
                         @RequestParam(value = "title", required = false) String title,
                         @RequestParam(value = "desc", required = false) String desc,
                         @RequestParam(value = "url", required = false) String url,
                         @RequestParam(value = "picurl", required = false) String picurl
    ) {

        SendMessageProxy sendMessageProxy = new SendMessageProxy();

        //文本消息
        if (MsgType.TEXT.getMsgtype().equals(msgtype)) {
            String ok = sendMessageProxy.sendKfTextMsg(openid, content);
            if ("OK".equals(ok)) {
                return 1;
            }
            //图文消息
        } else if (MsgType.NEWS.getMsgtype().equals(msgtype)) {
            String ok = sendMessageProxy.sendKfNewsMsg(openid, title, desc, url, picurl);
            if ("OK".equals(ok)) {
                return 1;
            }
        } else {
            System.out.println("暂时只支持文本和外链图文消息");
        }

        return 0;
    }


}
